package VLSys;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import VLSObjects.*;

public class ManagementUserAdd extends JPanel{
    
    JTextField username;
    JPasswordField password;
    JPasswordField password2;
    JComboBox designation;
    JButton submitButton;
    JButton cancelButton;
    JLabel usernameLabel;
    JLabel passwordLabel;
    JLabel password2Label;
    JLabel designationLabel;
    JLabel addUserLabel;
    
    JTextField surname;
    JTextField firstname;
    JLabel surnameLabel;
    JLabel firstnameLabel; 
    /*
    ImageIcon ask = new ImageIcon("images/prompt_ask.png");
    ImageIcon notice = new ImageIcon("images/prompt_notice.png");
    */
    
    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
    ImageIcon ask_red = new ImageIcon("images/prompt_ask_red.png");
    ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");
    
    String blank = "";
    String[] designationChoices = { "", "Cashier", "Manager"};
    String string[] = new String[6];
    
    public ManagementUserAdd(final JFrame frame)
    {
        
        setVisible(false);
        setLayout(null);
        setBackground(null);
        
        
        username = new JTextField();
        password = new JPasswordField();
        password2 = new JPasswordField();
        designation = new JComboBox(designationChoices);
        
        surname = new JTextField();
        firstname = new JTextField();
        
        submitButton = new JButton();
        cancelButton = new JButton();
        
        usernameLabel = new JLabel("Username:");
        passwordLabel = new JLabel("Password:");
        password2Label = new JLabel("Confirm Password:");
        designationLabel = new JLabel("Designation:");
        surnameLabel = new JLabel ("Surname:");
        firstnameLabel = new JLabel ("First Name:");
        addUserLabel = new JLabel("ADD USER");
        
        addUserLabel.setFont(new Font("sansserif", Font.PLAIN, 40));
        
        addUserLabel.setBounds(10,10,300,30);
        usernameLabel.setBounds(57,50,150,20);
        passwordLabel.setBounds(58,75,150,20);
        password2Label.setBounds(10,100,150,20);
        surnameLabel.setBounds(58,125,150,20);
        firstnameLabel.setBounds(58,150,150,20);
        designationLabel.setBounds(49,175,150,20);        
        
        username.setBounds(130,50,200,20);
        password.setBounds(130,75,200,20);
        password2.setBounds(130,100,200,20);
                
        surname.setBounds(130,125,200,20);
        firstname.setBounds(130,150,200,20);
        designation.setBounds(130,175,200,20);
        
        username.setBorder(null);
        password.setBorder(null);
        password2.setBorder(null);

        surname.setBorder(null);
        firstname.setBorder(null);
        designation.setBorder(null);
        
        
        submitButton.setText("Submit");
        cancelButton.setText("Clear");

        submitButton.setBounds(175,200,75,20);
        cancelButton.setBounds(255,200,75,20);
        
        
        add(username);
        add(password);
        add(password2);
        add(surname);
        add(firstname);
        add(designation);
        add(submitButton);
        add(cancelButton);
        add(usernameLabel);
        add(passwordLabel);
        add(password2Label);
        add(surnameLabel);
        add(firstnameLabel);
        add(designationLabel);
        add(addUserLabel);
        
        surname.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                
                if (e.getKeyCode()==39 || e.getKeyCode()==37)
                {

                }
                else 
                   surname.setText(capitalizeFirstLetters(surname.getText()));
          }

            public void keyTyped(KeyEvent e) {
              
            }

            public void keyPressed(KeyEvent e) {
              
            }
        }
        );
        
        firstname.addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                if (e.getKeyCode()==39 || e.getKeyCode()==37)
                {

                }
                else 
                    firstname.setText(capitalizeFirstLetters(firstname.getText()));
          }

          public void keyTyped(KeyEvent e) {
              
          }

          public void keyPressed(KeyEvent e) {
              
          }
        }
        );
        
       submitButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
                string[0] = username.getText(); 
                string[1] = new String(password.getPassword());
                string[2] = new String(password2.getPassword());
                string[3] = surname.getText(); 
                string[4] = firstname.getText(); 
                string[5] =(String)designation.getSelectedItem();
                
                if(string[0].length() == 0 && string[1].length() == 0 && string[2].length() == 0 && string[3].equals(blank))
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                        "Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                    
                } 
                else if(string[0].length() == 0 || string[1].length() == 0 || string[2].length() == 0 || string[3] == blank )
                {
                    checkEmpty();
                       if(string[3] != blank)
                                
                                if(string[0].length() == 0 && string[1].length() == 0 && string[2].length() == 0)
                                    JOptionPane.showMessageDialog(frame,
                                                "Incomplete Input",
                                                "Vehicle Logging Service",
                                                JOptionPane.WARNING_MESSAGE,
                                                notice_red);
                           
                                
                                else if(username.getText().length() < 6 || username.getText().length() > 10 && string[1].length() < 6 || string[1].length() > 10 && string[2].length() < 6 || string[2].length() > 10)
                                {
                                    JOptionPane.showMessageDialog(frame,
                                                "Invalid and Incomplete Input",
                                                "Vehicle Logging Service",
                                                JOptionPane.WARNING_MESSAGE,
                                                notice_red);
                                }
                                        
                        else if(username.getText().length() < 6 || username.getText().length() > 10 && string[1].length() < 6 || string[1].length() > 10 && string[2].length() < 6 || string[2].length() > 10)
                        {
                            JOptionPane.showMessageDialog(frame,
                                        "Invalid and Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);
                        }  
                        else
                            JOptionPane.showMessageDialog(frame,
                                        "Incomplete Input",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_red);                    
                }   
                else if(string[1].equals(string[2]) == false && username.getText().length() < 6 || username.getText().length() > 10 || string[1].length() < 6 || string[1].length() > 10 ||string[2].length() < 6 || string[3].length() > 10 || string[1].equals(string[2]) == false)
                {
                    checkEmpty();
                    JOptionPane.showMessageDialog(frame,
                                "Invalid Input",
                                "Vehicle Logging Service",
                                JOptionPane.WARNING_MESSAGE,
                                notice_red);                     
                }            
                else if(username.getText().length() >= 6 && username.getText().length() <= 10 &&  string[1].length() >= 6 && string[1].length() <= 10 && string[2].length() >= 6 && string[2].length() <= 10 && string[4].equals(blank))   
                {
                    int n = JOptionPane.showConfirmDialog(frame,
                                        "Proceed with current values?",
                                        "Vehicle Logging Service",
                                        JOptionPane.YES_NO_OPTION,
                                        JOptionPane.QUESTION_MESSAGE,
                                        ask_red);
                    
                    if(n==0)
                    {
                        
                        User newUser = new User();
                        userManager newUm = new userManager();

                        
                        newUser.setUsername(string[0]);
                        newUser.setPassword(string[1]);
                        newUser.setSurname(string[3]);
                        newUser.setFirstName(string[4]);
                        newUser.setDesignation(string[5]);
                        
                        
                       boolean existing = newUm.user_exist(string[0], "--");
                       //check if a similar record already exists
                           
                       if(existing == true) 
                           {    JOptionPane.showMessageDialog(frame,
                                            "User record already exists!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_red);
                           }
                           else
                           {   //add to database if no similar record is found
                              
                            boolean result = newUm.addUser(newUser);
                            if (result == true)
                                JOptionPane.showMessageDialog(frame,
                                        "User Added!",
                                        "Vehicle Logging Service",
                                        JOptionPane.WARNING_MESSAGE,
                                        notice_green);
                             }

                            resetValues();
                    }
                }
            }
        }
        );
       
       cancelButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
                int n = JOptionPane.showConfirmDialog(frame,
                                    "Are you sure you want to clear the values?",
                                    "Vehicle Logging Service",
                                    JOptionPane.YES_NO_OPTION,
                                    JOptionPane.QUESTION_MESSAGE,
                                    ask_red);
                if(n==0)
                { 
                    resetValues();
                }
            }
        }
        );   
    }
    
    public void checkEmpty()
    {   
        String s = "";
        String c = new String(password.getPassword());
        String c1 = new String(password2.getPassword());
       
        if(username.getText().length() == 0)
            username.setBackground(Color.YELLOW);
               
        else if(username.getText().length() < 6 || username.getText().length() > 10)
            username.setBackground(Color.RED);
                 
        if( username.getText().length() >= 6 && username.getText().length() <= 10)
            username.setBackground(null);
        
        if( c.length() == 0)
            password.setBackground(Color.YELLOW);
        
        else if(c.length() < 6 || c.length() > 10)
            password.setBackground(Color.RED);
        
        if( c.length() >= 6 && c.length() <= 10)
            password.setBackground(null);
        
        if( c1.length() == 0)
            password2.setBackground(Color.YELLOW);
        
        else if(c1.length() < 6 || c1.length() > 10)
            password2.setBackground(Color.RED);
        
        if( c1.length() >= 6 && c1.length() <= 10)
            password2.setBackground(null);
        
        if(c.equals(c1) == false)
        {
            password.setBackground(Color.RED);
            password2.setBackground(Color.RED);
        }

        
        if((String)designation.getSelectedItem() == s)
            designation.setBackground(Color.YELLOW);
                
        if((String)designation.getSelectedItem() != s)
            designation.setBackground(null);
        
    }
    
    public void resetValues()
    {
        username.setBackground(null);
        password.setBackground(null);
        password2.setBackground(null);

        surname.setEnabled(true);
        firstname.setEnabled(true);
        designation.setBackground(null);

        username.setText("");
        password.setText("");
        password2.setText("");

        surname.setText("");
        firstname.setText("");
        designation.setSelectedItem("");

    }
    
    public static String capitalizeFirstLetters ( String s ) {

        for (int i = 0; i < s.length(); i++) {

            if (i == 0) {
                // Capitalize the first letter of the string.
                s = String.format( "%s%s",
                             Character.toUpperCase(s.charAt(0)),
                             s.substring(1) );
            }

            // Is this character a non-letter or non-digit?  If so
            // then this is probably a word boundary so let's capitalize
            // the next character in the sequence.
            if (!Character.isLetterOrDigit(s.charAt(i))) {
                if (i + 1 < s.length()) {
                    s = String.format( "%s%s%s",
                                 s.subSequence(0, i+1),
                                 Character.toUpperCase(s.charAt(i + 1)),
                                 s.substring(i+2) );
                }
            }
        }
        return s;
    }
    
}
